﻿using System.Linq;
using T.FAS.Runtime.DataAccess.Engine.ValidateChecker.Entity;

namespace T.FAS.Runtime.DataAccess.Engine
{
    public class BooleanValidateFactory : CommonFactory
    {
        public override IColumnValidater GetDataTypeValidater()
        {
            return new BooleanDataTypeValidater();
        }
        public override IColumnValidater GetLengthValidater()
        {
            return new BooleanDataTypeValidater();
        }
    }

    public class BooleanDataTypeValidater : IColumnDataTypeValidater
    {
        public ValidateCheckItem Validate(ValidateCheckItem item)
        {
            if (item.Result.IsStop)
                return item;
            //数据类型验证
            var inputVal = item.InputValue.ToString();
            if (!bool.TryParse(inputVal, out bool value))
            {
                //兼容处理，值为0,1也可通过校验
                if (!new string[] { "0", "1" }.Contains(inputVal))
                {
                    item.Result.Flag = false;
                    item.Result.IsStop = true;
                    item.Result.Msg.Append(string.Format(ValidateConst.DataTypeErrorTemp, item.CheckElement.Name, inputVal, item.CheckElement.DataType));
                }
                else
                {
                    if (inputVal == "0")
                        item.ConvertValue = false;
                    if (inputVal == "1")
                        item.ConvertValue = true;
                }
            }
            else
            {
                item.ConvertValue = value;
            }
            return item;
        }
    }

    public class BooleanLengthValidater : IColumnLengthValidater
    {
        /// <summary>
        /// Boolean默认不用校验长度
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public ValidateCheckItem Validate(ValidateCheckItem item)
        {
            return item;
        }
    }

}
